package com.sanjiahantang.youjiaclient.module.common.views.impl;

/**
 * Created by zy on 2017/8/3.
 */


import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.base.Guide;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.module.common.beans.ScreenAdBean;
import com.sanjiahantang.youjiaclient.module.common.presenters.SplashPresenter;
import com.sanjiahantang.youjiaclient.module.common.views.SplashView;
import com.sanjiahantang.youjiaclient.utils.AdvertiseUtil;
import com.sanjiahantang.youjiaclient.utils.SPUtils;

import java.io.File;


public class SplashActivity extends BaseMvpActivity<SplashView, SplashPresenter> implements SplashView, View.OnClickListener {

    private final int MSG_LOGIN_SUCCESS = 100;
    private final int MSG_LOGIN_FAIL = 101;
    private final int MSG_VERSION_CHECK_TIMEOUT = 102;
    private final int UPDATE_TEAY_TIME = 103;

    private int delayTime = 4;// 广告4秒倒计时
    private ImageView welComeImg, adImg;
    private RelativeLayout rl;
    private TextView dTime;
    private TextView skipBtn;

    private boolean adIsFinish = false;


    @Override
    protected void onDestroy() {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler.removeMessages(MSG_VERSION_CHECK_TIMEOUT);
            handler = null;
        }
        super.onDestroy();
    }

    @Override
    public SplashPresenter initPresenter() {
        return new SplashPresenter();
    }

    @Override
    public void initView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //去掉标题栏，只去掉这一行不行，还有信息栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //去掉信息栏，设置全屏
        setContentView(R.layout.activity_splash);
        welComeImg = (ImageView) findViewById(R.id.iv_welcome_img);
        rl = (RelativeLayout) findViewById(R.id.rl_show_ad);
        rl.setOnClickListener(this);
        skipBtn = (TextView) findViewById(R.id.ll_ad_skip_btn);
        adImg = (ImageView) findViewById(R.id.iv_ad_img);
        dTime = (TextView) findViewById(R.id.tv_time);
    }

    @Override
    public boolean isInMultiWindowMode() {
        return false;
    }

    @Override
    public void initData() {
        String flag = (String) SPUtils.get($this, SPConfig.FIRST, "");

        if (!TextUtils.isEmpty(flag)) {
            ScreenAdBean bean = new Gson().fromJson((String) SPUtils.get($this, SPConfig.SCREEN_ADVERTISEMENT, "{}"), ScreenAdBean.class);
            if (bean.getUrl() != null) {
                File f = new File(bean.getUrl());
                if (f.exists()) {
                    adIsFinish = true;
                    Bitmap b = AdvertiseUtil.scaleImgSize(f);
                    adImg.setImageBitmap(b);
                    adIsFinish = true;
                    b = null;
                }
            }
        } else {
            SPUtils.put($this, SPConfig.FIRST, "1");
            Intent intent = new Intent($this, Guide.class);
            $this.startActivity(intent);
            $this.finish();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.loadScreenAdvisement($this);
        /**
         * 超时设置
         */
        handler.sendEmptyMessageDelayed(MSG_VERSION_CHECK_TIMEOUT, 1 * 1000);
    }


    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }

    private void jumpToMain() {
        Intent intent;
        intent = new Intent($this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private Handler handler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_LOGIN_SUCCESS:
                    jumpToMain();
                    break;
                case MSG_LOGIN_FAIL:

                    break;
                case MSG_VERSION_CHECK_TIMEOUT:
                    // 启动页面版本检查接口调用：3秒中没有返回，跳转页面
                    skipToLoginOrMain();
                    break;
                case UPDATE_TEAY_TIME:
                    if (adIsFinish) {
                        if (delayTime > 0) {
                            welComeImg.setVisibility(View.GONE);
                            rl.setVisibility(View.VISIBLE);
                            dTime.setText(delayTime + "");
                            handler.sendEmptyMessageDelayed(UPDATE_TEAY_TIME, 1000);
                            delayTime--;
                        } else {
                            jumpToMain();
                            finish();
                        }
                    } else {
                        jumpToMain();
                        finish();
                    }
                    break;
            }
        }
    };

    private void skipToLoginOrMain() {
        handler.sendEmptyMessageDelayed(UPDATE_TEAY_TIME, 1000);
    }

    @Override
    public void showProgress() {
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.rl_show_ad:
                handler.removeMessages(UPDATE_TEAY_TIME);
                jumpToMain();
                finish();
                break;
        }

    }
}
